﻿using System;
using System.Data;
using System.Text;
using System.Data.OracleClient;
using DBUtility;//请先添加引用
namespace FiberRMS.DAL.AlarmManager
{
    /// <summary>
    /// 数据访问类ALARM_MAP。
    /// </summary>
    public class ALARM_MAP
    {
        public ALARM_MAP()
        { }
        #region  成员方法
        public bool AddCause(FiberRMS.Model.AlarmManager.ALARM_MAP model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ALARM_MAP(");
            strSql.Append("ALARMMAPID,RESOURCEID,RESOURCENAME,PRIMALCAUSE,VENDOR)");
            strSql.Append(" values (");
            strSql.Append(":ALARMMAPID,:RESOURCEID,:RESOURCENAME,:PRIMALCAUSE,:VENDOR)");
            OracleParameter[] parameters = {
					new OracleParameter(":ALARMMAPID", OracleType.VarChar,10),
					new OracleParameter(":RESOURCEID", OracleType.VarChar,50),
					new OracleParameter(":RESOURCENAME", OracleType.VarChar,50),
					new OracleParameter(":PRIMALCAUSE", OracleType.VarChar,500),					
					new OracleParameter(":VENDOR", OracleType.VarChar,200)};
            parameters[0].Value = model.ALARMMAPID;
            parameters[1].Value = model.RESOURCEID;
            parameters[2].Value = model.RESOURCENAME;
            parameters[3].Value = model.PRIMALCAUSE;
            parameters[4].Value = model.VENDOR;
            bool Result = false;
            try
            {
                int i = OracleHelper.ExecuteSql(strSql.ToString(), parameters);
                if (i > 0)
                {
                    Result = true;
                }
            }
            catch (Exception ex)
            {
                XCLib.Log.CLogSingleton.PolicyOneLog(ex.Message, "添加告警原因失败！", strSql.ToString());
                Result = false;
            }
            return Result;
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ALARMMAPID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ALARM_MAP");
            strSql.Append(" where ALARMMAPID=:ALARMMAPID ");
            OracleParameter[] parameters = {
					new OracleParameter(":ALARMMAPID", OracleType.VarChar,50)};
            parameters[0].Value = ALARMMAPID;

            return OracleHelper.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 获得厂家
        /// </summary>
        /// <returns></returns>
        public DataSet getVotor()
        {
            string strSql = "select codeid,codevalue from code where codeid like '1003__' order by sort";
            return DBUtility.OracleHelper.Query(strSql);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(FiberRMS.Model.AlarmManager.ALARM_MAP model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ALARM_MAP(");
            strSql.Append("ALARMMAPID,RESOURCEID,RESOURCENAME,PRIMALCAUSE,ALARMCAUSE,ALARMLEVEL,ALARMTYPE,VENDOR)");
            strSql.Append(" values (");
            strSql.Append(":ALARMMAPID,:RESOURCEID,:RESOURCENAME,:PRIMALCAUSE,:ALARMCAUSE,:ALARMLEVEL,:ALARMTYPE,:VENDOR)");
            OracleParameter[] parameters = {
					new OracleParameter(":ALARMMAPID", OracleType.VarChar,10),
					new OracleParameter(":RESOURCEID", OracleType.VarChar,50),
					new OracleParameter(":RESOURCENAME", OracleType.VarChar,50),
					new OracleParameter(":PRIMALCAUSE", OracleType.VarChar,500),
					new OracleParameter(":ALARMCAUSE", OracleType.VarChar,500),
					new OracleParameter(":ALARMLEVEL", OracleType.VarChar,20),
					new OracleParameter(":ALARMTYPE", OracleType.VarChar,20),
					new OracleParameter(":VENDOR", OracleType.VarChar,200)};
            parameters[0].Value = model.ALARMMAPID;
            parameters[1].Value = model.RESOURCEID;
            parameters[2].Value = model.RESOURCENAME;
            parameters[3].Value = model.PRIMALCAUSE;
            parameters[4].Value = model.ALARMCAUSE;
            parameters[5].Value = model.ALARMLEVEL;
            parameters[6].Value = model.ALARMTYPE;
            parameters[7].Value = model.VENDOR;

            int i = OracleHelper.ExecuteSql(strSql.ToString(), parameters);
            if (i > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(FiberRMS.Model.AlarmManager.ALARM_MAP model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ALARM_MAP set ");
            strSql.Append("RESOURCEID=:RESOURCEID,");
            strSql.Append("RESOURCENAME=:RESOURCENAME,");
            strSql.Append("PRIMALCAUSE=:PRIMALCAUSE,");
            strSql.Append("ALARMCAUSE=:ALARMCAUSE,");
            strSql.Append("ALARMLEVEL=:ALARMLEVEL,");
            strSql.Append("ALARMTYPE=:ALARMTYPE,");
            strSql.Append("VENDOR=:VENDOR");
            strSql.Append(" where ALARMMAPID=:ALARMMAPID ");
            OracleParameter[] parameters = {
					new OracleParameter(":ALARMMAPID", OracleType.VarChar,80),
					new OracleParameter(":RESOURCEID", OracleType.VarChar,50),
					new OracleParameter(":RESOURCENAME", OracleType.VarChar,50),
					new OracleParameter(":PRIMALCAUSE", OracleType.VarChar,500),
					new OracleParameter(":ALARMCAUSE", OracleType.VarChar,500),
					new OracleParameter(":ALARMLEVEL", OracleType.VarChar,20),
					new OracleParameter(":ALARMTYPE", OracleType.VarChar,20),
					new OracleParameter(":VENDOR", OracleType.VarChar,200)};
            parameters[0].Value = model.ALARMMAPID;
            parameters[1].Value = model.RESOURCEID;
            parameters[2].Value = model.RESOURCENAME;
            parameters[3].Value = model.PRIMALCAUSE;
            parameters[4].Value = model.ALARMCAUSE;
            parameters[5].Value = model.ALARMLEVEL;
            parameters[6].Value = model.ALARMTYPE;
            parameters[7].Value = model.VENDOR;

            int i = OracleHelper.ExecuteSql(strSql.ToString(), parameters);
            if (i > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string ALARMMAPID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete ALARM_MAP ");
            strSql.Append(" where ALARMMAPID=:ALARMMAPID ");
            OracleParameter[] parameters = {
					new OracleParameter(":ALARMMAPID", OracleType.VarChar,50)};
            parameters[0].Value = ALARMMAPID;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FiberRMS.Model.AlarmManager.ALARM_MAP GetModel(string ALARMMAPID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ALARMMAPID,RESOURCEID,RESOURCENAME,PRIMALCAUSE,ALARMCAUSE,ALARMLEVEL,ALARMTYPE,VENDOR from ALARM_MAP ");
            strSql.Append(" where ALARMMAPID=:ALARMMAPID ");
            OracleParameter[] parameters = {
					new OracleParameter(":ALARMMAPID", OracleType.VarChar,50)};
            parameters[0].Value = ALARMMAPID;

            FiberRMS.Model.AlarmManager.ALARM_MAP model = new FiberRMS.Model.AlarmManager.ALARM_MAP();
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.ALARMMAPID = ds.Tables[0].Rows[0]["ALARMMAPID"].ToString();
                model.RESOURCEID = ds.Tables[0].Rows[0]["RESOURCEID"].ToString();
                model.RESOURCENAME = ds.Tables[0].Rows[0]["RESOURCENAME"].ToString();
                model.PRIMALCAUSE = ds.Tables[0].Rows[0]["PRIMALCAUSE"].ToString();
                model.ALARMCAUSE = ds.Tables[0].Rows[0]["ALARMCAUSE"].ToString();
                model.ALARMLEVEL = ds.Tables[0].Rows[0]["ALARMLEVEL"].ToString();
                model.ALARMTYPE = ds.Tables[0].Rows[0]["ALARMTYPE"].ToString();
                model.VENDOR = ds.Tables[0].Rows[0]["VENDOR"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ALARMMAPID,RESOURCEID,RESOURCENAME,PRIMALCAUSE,ALARMCAUSE,ALARMLEVEL,ALARMTYPE,VENDOR,updatedate ");
            strSql.Append(" FROM ALARM_MAP ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere );
            }
            return OracleHelper.Query(strSql.ToString() + " order by VENDOR,ALARMCAUSE  asc ");
        }
        /// <summary>
        /// 根据条件获得告警 对应CODE字典表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet getALLAlarmFromCommWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.ALARMMAPID,a.RESOURCEID,a.RESOURCENAME,a.PRIMALCAUSE,a.ALARMCAUSE,a.ALARMLEVEL,a.ALARMTYPE,a.VENDOR ");
            strSql.Append(" FROM ALARM_MAP a");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OracleHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OracleParameter[] parameters = {
                    new OracleParameter(":tblName", OracleType.VarChar, 255),
                    new OracleParameter(":fldName", OracleType.VarChar, 255),
                    new OracleParameter(":PageSize", OracleType.Number),
                    new OracleParameter(":PageIndex", OracleType.Number),
                    new OracleParameter(":IsReCount", OracleType.Clob),
                    new OracleParameter(":OrderType", OracleType.Clob),
                    new OracleParameter(":strWhere", OracleType.VarChar,1000),
                    };
            parameters[0].Value = "ALARM_MAP";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return OracleHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

